package com.runvp.rest.biz;

import com.runvp.common.annotation.Log;
import com.runvp.common.annotation.ParamValid;
import com.runvp.common.core.controller.BaseController;
import com.runvp.common.core.domain.AjaxResult;
import com.runvp.common.core.page.TableDataInfo;
import com.runvp.common.enums.BusinessType;
import com.runvp.common.utils.poi.ExcelUtil;
import com.runvp.service.biz.domain.BizGoods;
import com.runvp.service.biz.service.IBizGoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品列表Controller
 *
 * @author runvp
 * @date 2022-09-18
 */
@Controller
@RequestMapping("/biz/goods")
public class BizGoodsController extends BaseController {
    private String prefix = "biz/goods" ;

    @Autowired
    private IBizGoodsService bizGoodsService;

    @RequiresPermissions("biz:goods:view")
    @GetMapping()
    public String goods(ModelMap mmap,Long categoryId,String deviceSn) {
        mmap.put("showId", categoryId);
        mmap.put("deviceSn", deviceSn);
        return prefix + "/goods" ;
    }
    /**
     * 设备收费方案初始化
     */
    @RequiresPermissions("biz:fee:init")
    @Log(title = "设备收费方案初始化", businessType = BusinessType.INSERT)
    @GetMapping("/initFee")
    @ResponseBody
    public AjaxResult initFee(String deviceSn) {
        bizGoodsService.initFee(deviceSn);
        return toAjax(1);
    }
    /**
     * 查询商品列表
     */
    @RequiresPermissions("biz:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizGoods bizGoods) {
        startPage();
//        bizGoods.setTenantId(getUserIdFilterAdmin());
        List<BizGoods> list = bizGoodsService.selectBizGoodsList(bizGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @RequiresPermissions("biz:goods:export")
    @Log(title = "商品列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizGoods bizGoods) {
        bizGoods.setTenantId(getUserIdFilterAdmin());
        List<BizGoods> list = bizGoodsService.selectBizGoodsList(bizGoods);
        ExcelUtil<BizGoods> util = new ExcelUtil<BizGoods>(BizGoods.class);
        return util.exportExcel(list, "商品列表数据");
    }

    /**
     * 新增商品列表
     */
    @GetMapping("/add")
    public String add(ModelMap mmap,Long categoryId) {
        mmap.put("showId", categoryId);
        return prefix + "/add" ;
    }

    /**
     * 新增保存商品列表
     */
    @ParamValid
    @RequiresPermissions("biz:goods:add")
    @Log(title = "商品列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizGoods bizGoods) {
        bizGoods.setTenantId(getUserId());
        return toAjax(bizGoodsService.insertBizGoods(bizGoods));
    }

    /**
     * 修改商品列表
     */
    @RequiresPermissions("biz:goods:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BizGoods bizGoods = bizGoodsService.selectBizGoodsById(id);
        mmap.put("bizGoods", bizGoods);
        return prefix + "/edit" ;
    }

    /**
     * 查询商品信息
     * @param id
     */
    @ResponseBody
    @GetMapping("/get/{id}")
    public BizGoods get(@PathVariable("id") Long id) {
        BizGoods bizGoods = bizGoodsService.selectBizGoodsById(id);
        return bizGoods ;
    }
    /**
     * 修改保存商品列表
     */
    @ParamValid
    @RequiresPermissions("biz:goods:edit")
    @Log(title = "商品列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizGoods bizGoods) {
        return toAjax(bizGoodsService.updateBizGoods(bizGoods));
    }

    /**
     * 删除商品列表
     */
    @RequiresPermissions("biz:goods:remove")
    @Log(title = "商品列表", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(bizGoodsService.deleteBizGoodsByIds(ids));
    }
}
